﻿using DllCallAnalysis45;
using DrawIO45.Models.DrawIOs;
using DrawIO45.Models.Logic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DrawIO45.Draws.Business
{
    /// <summary>
    /// 花出来接口调用
    /// </summary>
    internal class DrawMethodCall : IDrawOnRoot
    {
        public void DrawOnRoot(Root root, List<DILocationInfo<LClassInfo>> classInfos)
        {
            var types = classInfos.Select(n => n.Object.Source).ToArray();
            var allMethods = classInfos.SelectMany(n => n.Object.MethodInfos).ToList();

            // 画函数的调用线
            foreach (var itemClassInfo in classInfos)
            {
                var methods = itemClassInfo.Object.MethodInfos;
                if (methods?.Any() != true)
                    continue;

                foreach (var itemMethod in methods)
                {
                    var callMethods = MethodAnalysisHelper.GetMethodCallMethods(types, itemMethod.Source);
                    if (callMethods?.Any() != true)
                        continue;

                    foreach (var itemCallMethod in callMethods)
                    {
                        var targetLMethod = allMethods.FirstOrDefault(n => n.Source == itemCallMethod);
                        if (targetLMethod == null)
                            continue;

                        root.MxCells.Add(new MxCell
                        {
                            Id = Guid.NewGuid().ToString(),
                            Parent = "1",
                            Style = "endArrow=open;endSize=12;dashed=1;html=1;rounded=0;exitX=0.447;exitY=0.004;exitDx=0;exitDy=0;exitPerimeter=0;",
                            Edge = "1",
                            Source = itemMethod.Id,
                            Target = targetLMethod.Id,
                            Value = "MethodCall",
                            MxGeometries = new List<MxGeometry>
                            {
                                new MxGeometry
                                {
                                    Width = "160",
                                    Relative = "1",
                                    As = "geometry",
                                },
                            },
                        });
                    }
                }
            }
        }
    }
}
